<template>
  <div>
    <div style="border: 1px solid #ccc">
      <Toolbar
        style="border-bottom: 1px solid #ccc"
        :editor="editor"
        :defaultConfig="toolbarConfig"
        :mode="mode"
      />
      <Editor
        style="height: 500px; overflow-y: hidden"
        v-model="html"
        :defaultConfig="editorConfig"
        :mode="mode"
        @onCreated="onCreated"
      />
    </div>
    <div class="tc mt15"> <el-button type="primary"  @click="submit">完 成</el-button> </div>
  </div>
</template>
 
<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { getToken } from '@/utils/auth'
export default {
  components: { Editor, Toolbar },
  props: {
    content: {//回显接口的数据
      type: String,
      default: ''
    }
  },
  data() {
    return {
      editor: null,
      html: '<p><br/></p>',
      toolbarConfig: {
        excludeKeys: ['fullScreen']
      },
      editorConfig: {
        placeholder: '请输入内容...',
        MENU_CONF: {
          uploadImage: {
            // server:放自己公司后端提供的图片上传接口
            server: process.env.VUE_APP_BASE_API + '/backstage/worker/file/v2/upload',
            // fieldName:自己公司和后端共同约定好的接收图片接口参数名
            fieldName: 'uploadFile',
            // 请求头
            headers: { Authorization: 'Bearer ' + getToken() },
            customInsert: (res, insertFn) => {
              insertFn(res.data.url)
            }
          }
        }
      },
      mode: 'default'
    }
  },
  methods: {
    submit() {
      this.$emit('submit', this.html)//提交富文本内容给后端定义的接口
      this.html = '<p><br/></p>'
    },
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
    }
  },
  mounted() {
    // 模拟 ajax 请求，异步渲染编辑器
    setTimeout(() => {
      this.content ? this.html = this.content : ''
    }, 500)
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy() // 组件销毁时，及时销毁编辑器
  }
}
</script>
 
<style src="@wangeditor/editor/dist/css/style.css"></style>